package org.tp8.sampleapp.service.impl;

import javax.jws.WebService;

import org.hibernate.Hibernate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.tp8.sampleapp.exc.SampleAppException;
import org.tp8.sampleapp.model.Course;
import org.tp8.sampleapp.persistence.repository.CourseRepository;
import org.tp8.sampleapp.rule.intf.RuleService;
import org.tp8.sampleapp.rule.model.RuleModelWrapper;
import org.tp8.sampleapp.service.abs.AbstractEntityService;
import org.tp8.sampleapp.service.intf.CourseService;
import org.tp8.sampleapp.util.SampleAppLogger;

/**
 * The Class CourseServiceImpl.
 */
@Transactional(propagation = Propagation.REQUIRED)
@Service(value = "courseService")
@WebService(endpointInterface = "org.tp8.sampleapp.service.intf.CourseService")
public class CourseServiceImpl extends
		AbstractEntityService<Long, Course, CourseRepository> implements
		CourseService {

	/**
	 * The Constant log.
	 */
	public final static SampleAppLogger log = new SampleAppLogger(
			CourseServiceImpl.class);

	/** The rule service. */
	@Autowired
	private RuleService ruleService;

	/**
	 * The course repository.
	 */
	@Autowired
	private CourseRepository courseRepository;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.tp8.sampleapp.service.abs.AbstractEntityService#getRepository()
	 */
	@Override
	public CourseRepository getRepository() {
		return courseRepository;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.tp8.sampleapp.service.BaseEntityService#save(org.tp8.sampleapp.model
	 * .AbstractModel)
	 */
	@Override
	public Course save(Course t) throws SampleAppException {
		return getRepository().save(t);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.tp8.sampleapp.service.BaseEntityService#findOne(java.io.Serializable)
	 */
	@Override
	public Course findOne(Long primaryKey) throws SampleAppException {
		Course result = getRepository().findOne(primaryKey);
		Hibernate.initialize(result.getStudents());
		Hibernate.initialize(result.getTeacher());
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.tp8.sampleapp.service.intf.CourseService#findAll(org.springframework
	 * .data.domain.Pageable)
	 */
	@Override
	public Page<Course> findAll(Pageable pageable) throws SampleAppException {
		Page<Course> result = getRepository().findAll(pageable);
		for (Course course : result) {
			Hibernate.initialize(course.getStudents());
		}
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.tp8.sampleapp.service.BaseEntityService#findByExample(org.springframework
	 * .data.domain.Pageable, org.tp8.sampleapp.model.AbstractModel)
	 */
	@Override
	public Page<Course> findByExample(Pageable pageable, Course example)
			throws SampleAppException {
		Page<Course> courses = getRepository()
				.queryByExample(pageable, example);
		for (Course course : courses) {
			Hibernate.initialize(course.getStudents());
		}
		return courses;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.tp8.sampleapp.service.BaseEntityService#delete(java.io.Serializable)
	 */
	@Override
	public void delete(Long primaryKey) throws SampleAppException {
		getRepository().delete(primaryKey);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.tp8.sampleapp.service.intf.CourseService#validate(org.tp8.sampleapp
	 * .model.Course)
	 */
	@Override
	public RuleModelWrapper validate(Course t) throws SampleAppException {
		return ruleService.execute("kbCourse", t);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.tp8.sampleapp.service.intf.CourseService#count()
	 */
	@Override
	public long count() throws SampleAppException {
		return getRepository().count();
	}
}
